#include "warn.h"
#include "db.h"
#include "global.h"
#include "u.h"
#include "user.h"
#include <qdatetime.h>
#include <qdebug.h>

Warn::Warn(QObject *parent)
    : QAbstractListModel(parent)
{
    qDebug() << __FUNCTION__ << __LINE__;
    // 可以立即加载初始数据
    DB* db = DB::getInstance();
    m_roles = db->getTableFieldRole("alarm_log");
    loadWarn("");
}

// bool Warn::addWarn(QString product, QString workShop)
// {
//     qDebug() << __FUNCTION__ << __LINE__;
//     User* pUser = (User*)Global::instance().get("userInfo");
//     QString loginName = pUser->currentLogin()["name"].toString();
//     QString realName = pUser->currentLogin()["real_name"].toString();
//     // DB::getInstance()->addUser(name, password, real_name, expiration, status, permission, create_by);
//     DB::getInstance()->addWarn(loginName, realName, m_WarnNo, product, workShop, U::getDateTimeSTD(), loginName);
//     m_product = product;
//     m_workShop = workShop;
//     loadWarn("");
//     return true;
// }

// bool Warn::updateWarn(QString desc)
// {
//     qDebug() << __FUNCTION__ << __LINE__;
//     DB::getInstance()->updateWarn(desc, U::getDateTimeSTD(), m_WarnNo, m_product, m_workShop);
//     loadWarn("");
//     return true;

// }

// QVariantMap Warn::get(int row) const
// {
//     qDebug() << __FUNCTION__ << __LINE__;
//     QVariantMap result;
//     if (row < 0 || row >= m_records.size())
//         return result;
//     const QHash<QString,QVariant> &record = m_records[row];
//     for (auto it = record.begin(); it != record.end(); ++it) {
//         result[it.key()] = it.value();
//     }
//     return result;
// }

int Warn::rowCount(const QModelIndex &parent) const
{
    // qDebug() << __FUNCTION__ << __LINE__;
    return m_records.size();
}

QVariant Warn::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() >= m_records.size())
        return QVariant();

    //qDebug() << __FUNCTION__ << __LINE__ << "role:" << role << "-index:" << index.row();
    const QHash<QString, QVariant> has = m_records[index.row()];

    return has[m_roles[role]];
}

QHash<int, QByteArray> Warn::roleNames() const
{
    qDebug() << __FUNCTION__ << __LINE__ ;
    return m_roles;
}

bool Warn::insertWarnLog(const QString &type, const QString &desc, const QString keyInfo)
{
    User* pUser = (User*)Global::instance().get("userInfo");
    QString loginName = pUser->currentLogin()["name"].toString();
    QString realName = pUser->currentLogin()["real_name"].toString();

    DB::getInstance()->addLog(loginName, realName, type, desc, keyInfo, loginName);
    return true;
}


// QString Warn::WarnNo()
// {
//     qDebug() << "WarnNo" << m_WarnNo;
//     return m_WarnNo;
// }

// void Warn::setWarnNo(QString WarnNo)
// {
//     qDebug() << "setWarnNo" << WarnNo;
//     this->m_WarnNo = WarnNo;
// }


bool Warn::loadWarn(const QString& filter)
{
    qDebug() << __FUNCTION__ << __LINE__;
    DB* db = DB::getInstance();
    beginResetModel();
    m_records = db->queryWarn(filter);
    endResetModel();
    return true;
}
